package com.auerdi;

import android.annotation.SuppressLint;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.TextView;

import com.auerdi.ui.detail.utils.Utils;
import com.auerdi.ui.fragment.MainFragment;
import com.auerdi.ui.fragment.c_officer.child.CDisPostFragment;
import com.example.eggframe.GoHomeEvent;
import com.example.eggframe.WechatEvent;
import com.example.egghttp.EggLog.L;
import com.jakewharton.rxbinding.view.RxView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.yokeyword.eventbusactivityscope.EventBusActivityScope;
import me.yokeyword.fragmentation.SupportActivity;
import me.yokeyword.fragmentation.anim.DefaultHorizontalAnimator;
import me.yokeyword.fragmentation.anim.FragmentAnimator;
import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;

public class MainActivity extends SupportActivity {

    @BindView(R.id.text)
    TextView text;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wechat_activity_main);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        Utils.setTranslucentMode(this);

//        IWXAPI api;
//        PayReq request = new PayReq();
//        request.appId = "wxd930ea5d5a258f4f";
//        request.partnerId = "1900000109";
//        request.prepayId= "1101000000140415649af9fc314aa427";
//        request.packageValue = "Sign=WXPay";
//        request.nonceStr= "1101000000140429eb40476f8896f4c9";
//        request.timeStamp= "1398746574";
//        request.sign= "7FFECB600D7157C5AA49810D2D8F28BC2811827B";
//        api.sendReq(request);




        if (findFragment(MainFragment.class) == null) {
            loadRootFragment(R.id.fl_container, MainFragment.newInstance());
        }

//        startActivity(new Intent(MainActivity.this, WXEntryActivity.class));// test sdk
    }

    private Observable<Integer> createObserver(int index) {
        return Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                for (int i = 1; i < 6; i++) {
                    subscriber.onNext(i * index);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).subscribeOn(Schedulers.newThread());
    }

    private Observable<Integer> combineLatestObserver() {
        return Observable.combineLatest(createObserver(1), createObserver(2), (num1, num2) -> {
            return num1 + num2;
        });
    }

    List<Observable<Integer>> list = new ArrayList<>();

    private Observable<Integer> combineListObserver() {
        for (int i = 1; i < 5; i++) {
            list.add(createObserver(i));
        }
        return Observable.combineLatest(list, args -> {
            int temp = 0;
            for (Object i : args) {
                temp += (Integer) i;
            }
            return temp;
        });
    }

    @SuppressLint("CheckResult")
    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);


        RxView.clicks(text)

                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe((Object btn) -> {
//                    L.d("hcia", "btn:" + btn);


                });

//        Observable<String> bb = Observable.just("bbb");
//        Observable<String> aa = Observable.just("aaa");
//        Observable<String> cc = Observable.just("ccc");
//        Observable.concat(bb, aa, cc).startWith("dd").subscribe(new Action1<String>() {
//            @Override
//            public void call(String s) {
////                L.d("hcia", "s:" + s);
//                text.setText(s);
//            }
//        });
//
//        Observable<Long> tictoc = Observable.interval(1, TimeUnit.SECONDS).delay(5, TimeUnit.SECONDS);
//
//        tictoc.subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Action1<Long>() {
//                    @Override
//                    public void call(Long aLong) {
////                        L.d("hcia", "aLong:" + aLong);
//
//                        text.setText("" + aLong);
//
//
//                    }
//                });
//
//        Observable.zip(tictoc, bb, aa, cc, (s, s2, s3, tt) -> s + s2 + s3 + " :" + tt).subscribe(s -> L.d("hcia", "s:" + s));


    }

    @Override
    public void onBackPressedSupport() {
        // 对于 4个类别的主Fragment内的回退back逻辑,已经在其onBackPressedSupport里各自处理了
        super.onBackPressedSupport();
    }

    @Override
    public FragmentAnimator onCreateFragmentAnimator() {
        // 设置横向(和安卓4.x动画相同)
        return new DefaultHorizontalAnimator();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        verifyCodeObservable.unsubscribeOn(AndroidSchedulers.mainThread()); //防止泄露
    }

    @Subscribe
    public void GoHomeEvent(WechatEvent event) {
        L.d("hcia", "MAinactivity event:" + event);


        if (event.getTaa() == 0) {
            text.postDelayed(new Runnable() {
                @Override
                public void run() {
                    EventBusActivityScope.getDefault(MainActivity.this).post(new GoHomeEvent());

                }
            }, 300);
        }


        if (event.getTaa() == 1) {
            text.postDelayed(new Runnable() {
                @Override
                public void run() {
                    EventBusActivityScope.getDefault(MainActivity.this).post(new GoHomeEvent(1));

                }
            }, 300);
        }


//        EventBus.getDefault().post(new GoHomeEvent());
//        MainFragment aa = findFragment(MainFragment.class);
//        popTo(aa.getClass(), false);
//        mBottomBar.setCurrentItem(0);


    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("photoPath", String.valueOf(CDisPostFragment.uriPho));
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (TextUtils.isEmpty(String.valueOf(CDisPostFragment.uriPho))) {
            CDisPostFragment.uriPho = Uri.parse(savedInstanceState.getString("photoPath"));
        }
    }


}
